﻿(function ($) {
    $.fn.ch2Pager = function (options) {
        var defaults = {
            pagesize: 15,
            pageindex: 1,
            showpageradius: 5,
            params: {},
            onClickItem: function (total,index,pagesize,params) { },
            unit:'位'
        };
        $.extend(defaults, options);
        options = defaults;
        var total = 0;
        var pageindex = options.pageindex;
        var pagesize = options.pagesize;
        var params = options.params;
        var $pager = $(this);
        if (!$pager.hasClass('ch2-pager')) {
            $pager.addClass('ch2-pager');
        }
        var $pagerLink = $('<div class="pager-link"></div>');
        var $pagerInfo = $('<div class="pager-info"></div>');
        $pagerLink.appendTo($pager);
        $pagerInfo.appendTo($pager);
        var _getPageCount = function () {
            return (total % pagesize == 0) ? (total / pagesize) : (parseInt((total / pagesize) + '') + 1);
        };
        var _setPagerInfo = function () {
            $pagerInfo.html([
                '<span>总共' + total + options.unit+',</span>',
                '<span>第' + pageindex + '页/共' + _getPageCount(total) + '页,</span>',
                '<span>每页'+pagesize+options.unit+'</span>'
            ].join(''));
        };
        var _createPageLink = function () {
            var pageCount = _getPageCount(total);
            pageCount = pageCount ? pageCount : 1;
            var startindex = ((pageindex - options.showpageradius) > 0) ? (pageindex - options.showpageradius) : 1;
            var endindex = ((pageindex + options.showpageradius) > pageCount) ? pageCount : (pageindex + options.showpageradius);
            $pagerLink.empty();
            if (startindex > 1) {
                var $firstLink = $('<a href="javascript:void(0);"><i class="fa fa-angle-double-left"></i></a>');
                $firstLink.data('index', 1);
                $firstLink.appendTo($pagerLink);
                var $prevLink = $('<a href="javascript:void(0);"><i class="fa fa-angle-left"></i></a>');
                $prevLink.data('index', pageindex - 1);
                $prevLink.appendTo($pagerLink);
            }
            for (var i = startindex; i <= endindex; i++) {
                var link = '<a href="javascript:void(0);">';
                link += i;
                link += '</a>';
                var $link = $(link);
                $link.data('index', i);
                $link.appendTo($pagerLink);
                if (i == pageindex) {
                    $link.addClass('active');
                }
            }
            if (endindex < pageCount) {
                var $nextLink = $('<a href="javascript:void(0);"><i class="fa fa-angle-right"></i></a>');
                $nextLink.data('index', pageindex + 1);
                $nextLink.appendTo($pagerLink);
                var $lastLink = $('<a href="javascript:void(0);"><i class="fa fa-angle-double-right"></i></a>');
                $lastLink.data('index', pageCount);
                $lastLink.appendTo($pagerLink);
            }
        };
        $pagerLink.on('click', 'a:not(.active)', function () {
            options.onClickItem(total,$(this).data('index'),pagesize,params);
        });
        var _setData = function (t, i, s, p) {
            total = t;
            pageindex = i;
            pagesize = s;
            params = p;
            _createPageLink();
            _setPagerInfo();
        };

        _setData(total, pageindex,pagesize,params);

        var resultObj = {
            elm: $pager,
            setData: function (total, index, pagesize,params) {
                _setData(total, index, pagesize, params);
                return this;
            }
        };
        return resultObj;
    };
})(jQuery);